package com.mdd.front.service.time.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.mdd.common.core.PageResult;
import com.mdd.common.entity.time.OrderTime;
import com.mdd.common.mapper.time.OrderTimeMapper;
import com.mdd.common.utils.TimeUtil;
import com.mdd.front.service.time.IOrderTimeService;
import com.mdd.front.validate.PageParam;
import com.mdd.front.vo.time.OrderTimeListVo;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Arrays;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

/**
 * 订单预约时间实现类
 */
@Service
public class OrderTimeServiceImpl implements IOrderTimeService {

    @Resource
    OrderTimeMapper orderTimeMapper;

    /**
     * 订单预约时间列表
     *
     * @param pageParam 分页参数
     * @param params    搜索参数
     * @return PageResult<OrderTimeListVo>
     */
    @Override
    public PageResult<OrderTimeListVo> list(PageParam pageParam, Map<String, String> params) {
        Integer page = pageParam.getPageNo();
        Integer limit = pageParam.getPageSize();

        QueryWrapper<OrderTime> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("is_delete", 0);
        queryWrapper.orderByAsc("id");

        orderTimeMapper.setSearch(queryWrapper, params, new String[]{
                "datetime:startTimeStart-startTimeEnd@start_time:str",
                "datetime:endTimeStart-endTimeEnd@end_time:str",
                "=:sort:int",
        });

        IPage<OrderTime> iPage = orderTimeMapper.selectPage(new Page<>(page, limit), queryWrapper);

        List<OrderTimeListVo> list = new LinkedList<>();
        for (OrderTime item : iPage.getRecords()) {
            OrderTimeListVo vo = new OrderTimeListVo();
            BeanUtils.copyProperties(item, vo);
            vo.setCreateTime(TimeUtil.timestampToDate(item.getCreateTime()));
            vo.setUpdateTime(TimeUtil.timestampToDate(item.getUpdateTime()));
            vo.setStartTime(item.getStartTime());
            vo.setEndTime(item.getEndTime());
            vo.setTimeDesc(item.getStartTime() + " - " + item.getEndTime());
            list.add(vo);
        }
        return PageResult.iPageHandle(iPage.getTotal(), iPage.getCurrent(), iPage.getSize(), list);
    }

}
